//
//  CaiPuLikeMoreViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CaiPuLikeMoreViewController.h"
#import "CaiPuRenQiTableViewCell.h"
#import "FitHeader.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
#import "HTTPMethod.h"
#import "changeString.h"
#import "ZJHShakeCaipuViewController.h"
#import "ZJHShakeModel.h"
#import "Loading.h"
@interface CaiPuLikeMoreViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, strong)Loading *load;
@property(nonatomic, assign)NSInteger offset;
@end

@implementation CaiPuLikeMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"猜你喜欢";
    self.navigationController.navigationBar.translucent = NO;
    [self getData];
}
-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    _offset = 0;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        _offset = 0;
        [self getData];
    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _offset += 15;
        [self getData];
    }];
    [self createProgressView];
}
//创建progressView
-(void)createProgressView
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < 70; i++) {
        [array addObject:[NSString stringWithFormat:@"%ld", i + 1]];
    }
    self.view.backgroundColor = [UIColor grayColor];
    _load = [[Loading alloc]initWithFrame:CGRectMake(0, 0, 375 * wt, 667 * ht - 64 - 49) PicArray:array];
    [self.view addSubview:_load];
}
#pragma mark 解析数据
-(void)getData
{
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSInteger date = time;
    NSString *post1 = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=dfdb41631f3ac3ea59a9b9b5d418d4c0&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7CCC2D6423-97F6-437A-B8DE-CBB86A71C6F1%7C608CBAFB-426B-4C02-96CA-F5B029C17D76&format=json&loguid=&method=Recipe.getCollectRecomment&nonce=1446732533&sessionid=1446731191&signmethod=md5&";
    NSString *post2 = [NSString stringWithFormat:@"timestamp=%ld&uuid=f54ccedd4a37e51b4340c5dc18fe353c&v=2&vc=43&vn=v5.4.0?", date];
    NSString *postString = [NSString stringWithFormat:@"%@%@", post1, post2];
    NSString *bodyString = [NSString stringWithFormat:@"Title=猜你喜欢&limit=15&offset=%ld&rid=&sign=&type=猜你喜欢&uid=&uuid=f54ccedd4a37e51b4340c5dc18fe353c", _offset];
    [HTTPMethod getDataByString:postString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *allDic = [data objectForKey:@"result"];
        NSArray *listArray = [allDic objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            ZJHRenQiModel *model = [[ZJHRenQiModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_load removeFromSuperview];
    }];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CaiPuRenQiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CaiPuRenQiTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.model = _dataArray[indexPath.row];
    }
#pragma mark cell动画
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190 * ht;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZJHShakeCaipuViewController *VC = [[ZJHShakeCaipuViewController alloc]init];
    ZJHShakeModel *model = [[ZJHShakeModel alloc]init];
    ZJHRenQiModel *renqiModel = _dataArray[indexPath.row];
    model.RecipeId = renqiModel.RecipeId;
    model.Title = renqiModel.Title;
    VC.tempModel = model;
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
